﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Collections;
using System.Text;
using IDAL;
using DAL;
using Models;

namespace BLL
{
    public class WorksInfoLogic
    {
        public static IWorksInfo worksinfo = new WorksInfoDAL();

        /// <summary>
        /// CategoryID 为0时为所有的works
        /// </summary>
        /// <param name="CategoryID"></param>
        /// <param name="pagesize"></param>
        /// <param name="nowpage"></param>
        /// <returns></returns>
        public static List<WorksInfo> GetWorksInfo(int CategoryID,int pagesize,int nowpage)
        {
            if(CategoryID == 0){
                return GetAllWorksInfo(pagesize, nowpage);
            }else{
                return GetCategoryWorksInfo(CategoryID, pagesize, nowpage);
            }
        }
        public static List<WorksInfo> GetAllWorksInfo(int pagesize, int nowpage)
        {
            List<WorksInfo> list = worksinfo.GetAllWorksInfo(pagesize, nowpage);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].ShowID = i + 1 + (nowpage - 1) * pagesize;
            }
            list = FormatWorksInfoList(list);
            return list;
        }
        public static List<WorksInfo> GetCategoryWorksInfo(int CategoryID, int pagesize, int nowpage)
        {
            List<WorksInfo> list = worksinfo.GetCategoryWorksInfo(CategoryID, pagesize, nowpage);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].ShowID = i + 1 + (nowpage - 1) * pagesize;
            }
            list = FormatWorksInfoList(list);
            return list;
        }

        public static List<WorksInfo> GetOneWorksInfo(int WorksID)
        {
            List<WorksInfo> list = worksinfo.GetOneWorksInfo(WorksID);
            string firstPicURL = worksinfo.GetWorksFirstPic(WorksID);
            for (int i = 0; i < list.Count; i++)
            {
                list[i].FirstPicURL = firstPicURL;
            }
            return list;
        }

        public static int GetPageCount(int pagesize,int CategoryID)
        {
            return worksinfo.GetPageCount(pagesize,CategoryID);
        }

        public static ArrayList GetPreWorksInfo(int nowWorksID,int categoryID)
        {
            return worksinfo.GetPreWorksInfo(nowWorksID,categoryID); 
        }

        public static ArrayList GetNextWorksInfo(int nowWorksID,int categoryID)
        {
            return worksinfo.GetNextWorksInfo(nowWorksID,categoryID);
        }

        public static List<WorksInfo> FormatWorksInfoList(List<WorksInfo> list)
        {
            
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].WorksBrief == "")
                {
                    string oldString = list[i].WorkContent;
                    string newString = DeleteHTML(oldString);
                    if (newString.Length > 55)
                    {
                        list[i].WorksBrief = newString.Substring(0, 55) + "……";
                    }
                    else
                    {
                        list[i].WorksBrief = newString;
                    }
                }
                if (list[i].HotFlag == 1)
                {
                    list[i].WorkHot = "<div class='workshot'><img src='images/bghotw.png' alt='hot news' /></div>";
                }
                else
                {
                    list[i].WorkHot = "";
                }
                if (list[i].IsNWorks == 1)
                {
                    list[i].WorkNew = "<div class='worksnf'></div>";
                }
                else
                {
                    list[i].WorkNew = "";
                }
            }
            return list;
        }
        public static string DeleteHTML(string Htmlstring)
        {
            string[] aryReg ={   
                      @"<script[^>]*?>.*?</script>",   
    
                      @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",   
                      @"([\r\n])[\s]+",   
                      @"&(quot|#34);",   
                      @"&(amp|#38);",   
                      @"&(lt|#60);",   
                      @"&(gt|#62);",     
                      @"&(nbsp|#160);",     
                      @"&(iexcl|#161);",   
                      @"&(cent|#162);",   
                      @"&(pound|#163);",   
                      @"&(copy|#169);",   
                      @"&#(\d+);",   
                      @"-->",   
                      @"<!--.*\n",
                      @"[ ]+"
                    };

            string[] aryRep = {   
                        "",   
                        "",   
                        "",   
                        "\"",   
                        "&",   
                        "<",   
                        ">",   
                        "   ",   
                        "\xa1",//chr(161),   
                        "\xa2",//chr(162),   
                        "\xa3",//chr(163),   
                        "\xa9",//chr(169),   
                        "",   
                        "\r\n",   
                        "",
                        " "
                      };

            string newReg = aryReg[0];
            string strOutput = Htmlstring;
            for (int i = 0; i < aryReg.Length; i++)
            {
                Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
                strOutput = regex.Replace(strOutput, aryRep[i]);
            }
            strOutput.Replace("<", "");
            strOutput.Replace(">", "");
            strOutput.Replace("\r\n", "");
            return strOutput;
        }
        public static int CountAddWorkInfo(int WorksID)
        {
            return worksinfo.CountAddWorkInfo(WorksID);
        }

        public static int AddWorksInfo(WorksInfo work)
        {
            return worksinfo.AddWorksInfo(work);
        }
        public static int GetMaxID()
        {
            return worksinfo.GetMaxID();
        }
        public static WorksInfo GetWorksInfo(int wid)
        {
            return worksinfo.GetWorksInfo(wid);
        }
        public static int UpdateWorksInfo(WorksInfo work)
        {
            return worksinfo.UpdateWorksInfo(work);
        }
        public static int DeleteWorksInfo(int WorksID)
        {
            return worksinfo.DeleteWorksInfo(WorksID);
        }
    }
}
